package com.compute.controller;

import com.compute.bean.FixedEvent;
import com.compute.bean.Task;
import com.compute.service.EventManageService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author 李天航
 * 活动安排的接口
 */
@RestController
@RequestMapping("/task")
public class TaskManageController {
    @Resource
    EventManageService eventManageService;

    @PostMapping("/manage")
    LinkedHashMap<String, Object> taskManage(@RequestBody LinkedHashMap<String, Object> params)
    {
        List<FixedEvent> alreadyIn= (List<FixedEvent>) params.get("fixedEvents");
        List<FixedEvent> fixedEvents=new ObjectMapper().convertValue(alreadyIn, new TypeReference<List<FixedEvent>>(){});
        List<Task> tasks = (List<Task>) params.get("tasks");
        List<Task> taskList=new ObjectMapper().convertValue(tasks, new TypeReference<List<Task>>() {});
        if(fixedEvents!=null&&tasks!=null) {
            String algorithmName = (String) params.getOrDefault("algorithmName","loadBalanceFirst");
            return eventManageService.eventManage(fixedEvents,taskList, algorithmName);
        }
        else
        {
            LinkedHashMap<String, Object> resultMap = new LinkedHashMap<>();
            resultMap.put("status",500);
            resultMap.put("msg","参数不全");
            return resultMap;
        }
    }
}
